package com.wentao.mall.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wentao.mall.commons.Result;
import com.wentao.mall.filter.CustomerFilter;
import com.wentao.mall.model.dto.CreateOrderRequest;
import com.wentao.mall.model.po.User;
import com.wentao.mall.model.vo.OrderVo;
import com.wentao.mall.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping("/order/create")
    public Result createOrder(@RequestBody @Valid CreateOrderRequest createOrderRequest){
        User user = CustomerFilter.currentUser;
        String orderNo=orderService.createOrder(createOrderRequest,user);
        return Result.success(orderNo);
    }

    @ApiOperation("订单详情")
    @GetMapping("/order/detail")
    public Result orderDetail(@RequestParam("orderNo") String orderNo){
        User user = CustomerFilter.currentUser;
        OrderVo orderVo = orderService.orderDetail(orderNo,user.getId());
        return Result.success(orderVo);
    }

    @ApiOperation("订单列表")
    @GetMapping("/order/list")
    public Result orderList(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        User user = CustomerFilter.currentUser;
        PageHelper.startPage(pageNum,pageSize);
        List<OrderVo> orderVoList = orderService.orderList(user.getId());
        PageInfo pageInfo = new PageInfo(orderVoList);
        return Result.success(pageInfo);
    }

    @ApiOperation("取消订单")
    @PostMapping("/order/cancel")
    public Result cancelOrder(@RequestParam("orderNo") String orderNo){
        User user = CustomerFilter.currentUser;
        orderService.cancelOrder(orderNo,user.getId());
        return Result.success("取消成功");
    }

    @ApiOperation("生成支付二维码")
    @GetMapping("/order/qrcode")
    public Result orderQrcode(@RequestParam("orderNo") String orderNo){
        User user = CustomerFilter.currentUser;
        String payUrl=orderService.orderQrcode(orderNo);
        return Result.success(payUrl);
    }

    @ApiOperation("用户支付订单")
    @GetMapping("/pay")
    public Result payOrder(@RequestParam("orderNo") String orderNo){
        orderService.pay(orderNo);
        return Result.success("支付成功");
    }
}
